/**
 * 
 */
package mblog.core.persist.dao.impl;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import mblog.core.persist.dao.FollowListDao;
import mtons.modules.persist.impl.DaoImpl;
import mtons.modules.pojos.Page;

import com.faxsun.core.persist.entity.FollowListPO;

/**
 * @author leima
 *
 */
public class FollowListDaoImpl extends DaoImpl<FollowListPO> implements FollowListDao {
	private static final long serialVersionUID = -3561107849207517664L;

	public FollowListDaoImpl() {
		super(FollowListPO.class);
	}

    @Override
    public List<FollowListPO> findAllMyFollowers(Page page, long uid, int itemType) {
        PagingQuery<FollowListPO> q = pagingQuery(page);
        q.add(Restrictions.eq("uid", uid));
        q.add(Restrictions.eq("item_type", itemType));
        q.add(Restrictions.eq("status", 0));
        q.desc("follower_id");
        return q.list();
    }

    @Override
    public List<FollowListPO> findAllMyFollows(Page page, long follower_id, int itemType) {
        PagingQuery<FollowListPO> q = pagingQuery(page);
        q.add(Restrictions.eq("follower_id", follower_id));
        q.add(Restrictions.eq("item_type", itemType));
        q.add(Restrictions.eq("status", 0));
        q.desc("uid");
        return q.list();
    }

    @Override
    public int countAllMyFollowers(long uid, int itemType) {    
        Criteria count = this.createCriteria();
        count.add(Restrictions.eq("uid", uid));
        count.add(Restrictions.eq("item_type", itemType));
        count.add(Restrictions.eq("status", 0));

        count.setProjection(Projections.count("id"));
        Long total = (Long) count.list().get(0);
        return total.intValue();
    }

    @Override
    public int countAllMyFollows(long follower_id, int itemType) {   
        Criteria count = this.createCriteria();
        count.add(Restrictions.eq("follower_id", follower_id));
        count.add(Restrictions.eq("item_type", itemType));
        count.add(Restrictions.eq("status", 0));

        count.setProjection(Projections.count("id"));
        Long total = (Long) count.list().get(0);
        return total.intValue();
    }

    @Override
    public FollowListPO findFollow(long uid, long follower_id, int itemType) {
        TopQuery<FollowListPO> q = topQuery(1);
        q.add(Restrictions.eq("uid", uid));
        q.add(Restrictions.eq("follower_id", follower_id));
        q.add(Restrictions.eq("item_type", itemType));
        List<FollowListPO> list = q.list();
        return (list != null && list.size() > 0) ? list.get(0) : null;
    }

    @Override
    public int countAllMyThumbsupers(long uid, int itemType) {   
        Criteria count = this.createCriteria();
        count.add(Restrictions.eq("uid", uid));
        count.add(Restrictions.eq("item_type", itemType));
        count.add(Restrictions.eq("thumbsup", true));
        count.setProjection(Projections.count("id"));
        Long total = (Long) count.list().get(0);
        return total.intValue();
    }
	
}
